<template>
  <div>
    <!-- 面包屑导航 -->
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ name: 'AdminHost' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
    </el-breadcrumb> -->
    <!-- 搜索筛选 -->
    <el-card style="min-height: 400px">
      <el-form :inline="true" :model="formInline" class="user-search">
        <el-form-item label="搜索："> </el-form-item>
        <el-form-item label="">
          <el-input
            size="small"
            v-model="search"
            placeholder="输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-input
            size="small"
            v-model="mobileSearch"
            placeholder="输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-position"
            @click="goHostApply()"
            >查看申请</el-button
          >
        </el-form-item>
      </el-form>
      <!--列表-->
      <el-table
        size="small"
        :data="
          hostData.filter(
            (data) =>
              (!search ||
                data.user.name.toLowerCase().includes(search.toLowerCase())) &&
              (!mobileSearch || data.account.mobile.includes(mobileSearch))
          )
        "
        @selection-change="selectChange"
        highlight-current-row
        v-loading="loading"
        border
        element-loading-text="拼命加载中"
        style="width: 100%"
      >
        <el-table-column align="center" prop="address" label="住址" width="120">
          <template #default="scope">
            <div>{{ scope.row.host.address }}</div>
          </template>
        </el-table-column>

        <el-table-column align="center" prop="name" label="姓名" width="120">
          <template #default="scope">
            <div>{{ scope.row.user.name }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="sex"
          label="性别"
          filter-placement="bottom-end"
          :filters="[
            { text: '女', value: '女' },
            { text: '男', value: '男' },
          ]"
          :filter-method="sexFilter"
          min-width="70"
        >
          <template #default="scope">
            <div>{{ scope.row.user.sex }}</div>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          prop="tel"
          label="手机号码"
          min-width="100"
        >
          <template #default="scope">
            <div>{{ scope.row.account.mobile }}</div>
          </template>
        </el-table-column>

        <el-table-column label="操作" min-width="100" align="center">
          <template #default="scope">
            <el-button size="mini" @click="detailInfo(scope.$index, scope.row)"
              >查看信息</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      :title="title"
      v-model="detailFormVisible"
      width="30%"
      @click="closeDialog('edit')"
    >
      <el-form
        label-width="80px"
        align="center"
        ref="detailForm"
        :model="detailForm"
        :rules="rules"
      >
        <el-form-item label="姓名" prop="name">
          {{ detailForm.user.name }}
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          {{ detailForm.user.age }}
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          {{ detailForm.user.sex }}
        </el-form-item>
        <el-form-item label="住址" prop="address">
          {{ detailForm.user.address }}
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          {{ detailForm.account.mobile }}
        </el-form-item>
        <el-form-item label="身份证号" prop="idNumber">
          {{ detailForm.user.idNumber }}
        </el-form-item>
        <el-form-item label="创建时间" prop="dateTime">
          {{ detailForm.host.date_time }}
        </el-form-item>
        <el-form-item label="描述" prop="description">
          {{ detailForm.user.description }}
        </el-form-item>
      </el-form>
      <div class="dialog-footer" align="center">
        <el-button size="small" @click="detailFormVisible = false"
          >取消</el-button
        >
        <!-- <el-button
          size="small"
          type="primary"
          :loading="loading"
          class="title"
          @click="submitForm('detailForm')"
          >保存</el-button
        > -->
      </div>
    </el-dialog>
    <!-- 数据权限 -->
  </div>
</template>

<script>
import { getHost, getAll } from "@/network/host";

export default {
  name: "HostManage",
  data() {
    return {
      isChange: true,
      loading: false, //是显示加载
      title: "添加用户",
      detailFormVisible: false, //控制编辑页面显示与隐藏
      // 编辑与添加
      detailForm: {},
      formInline: {},
      hostData: [],
      search: "",
      mobileSearch: "",
    };
  },
  created() {
    this.getHostData();
  },
  beforeUpdate() {
    // this.getHostData();
  },
  methods: {
    getHostData() {
      getAll().then(
        (res) => {
          this.hostData = res.data;
        },
        (err) => {
          this.$message.error("请求失败");
        }
      );
    },
    goHostApply() {
      this.$router.push({ name: "AdminManageHost" });
    },
    detailInfo(index, row) {
      this.detailFormVisible = true;
      this.detailForm = row;
    },
    sexFilter(value, row) {
      return row.user.sex === value;
    },
  },
};
</script>

<style scoped>
</style>